package com.ruoyi.lowcode.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.lowcode.domain.VoteActivity;
import com.ruoyi.lowcode.service.IVoteActivityService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 投票活动Controller
 * 
 * @author ruoyi
 * @date 2023-06-28
 */
@Controller
@RequestMapping("/vote/activity")
public class VoteActivityController extends BaseController
{
    private String prefix = "vote/activity";

    @Autowired
    private IVoteActivityService voteActivityService;

    @RequiresPermissions("vote:activity:view")
    @GetMapping()
    public String activity()
    {
        return prefix + "/activity";
    }

    /**
     * 查询投票活动列表
     */
    @RequiresPermissions("vote:activity:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VoteActivity voteActivity)
    {
        startPage();
        List<VoteActivity> list = voteActivityService.selectVoteActivityList(voteActivity);
        return getDataTable(list);
    }

    /**
     * 导出投票活动列表
     */
    @RequiresPermissions("vote:activity:export")
    @Log(title = "投票活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VoteActivity voteActivity)
    {
        List<VoteActivity> list = voteActivityService.selectVoteActivityList(voteActivity);
        ExcelUtil<VoteActivity> util = new ExcelUtil<VoteActivity>(VoteActivity.class);
        return util.exportExcel(list, "投票活动数据");
    }

    /**
     * 新增投票活动
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存投票活动
     */
    @RequiresPermissions("vote:activity:add")
    @Log(title = "投票活动", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VoteActivity voteActivity)
    {
        return toAjax(voteActivityService.insertVoteActivity(voteActivity));
    }

    /**
     * 修改投票活动
     */
    @RequiresPermissions("vote:activity:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        VoteActivity voteActivity = voteActivityService.selectVoteActivityById(id);
        mmap.put("voteActivity", voteActivity);
        return prefix + "/edit";
    }

    /**
     * 修改保存投票活动
     */
    @RequiresPermissions("vote:activity:edit")
    @Log(title = "投票活动", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VoteActivity voteActivity)
    {
        return toAjax(voteActivityService.updateVoteActivity(voteActivity));
    }

    /**
     * 删除投票活动
     */
    @RequiresPermissions("vote:activity:remove")
    @Log(title = "投票活动", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(voteActivityService.deleteVoteActivityByIds(ids));
    }
}
